package com.smart.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smart.model.Admin;
import com.smart.model.User;
import com.smart.service.HouseService;
import com.smart.service.UserService;

@Controller
public class UserOperController {
	@RequestMapping(value="/pc/get_user_list", method = RequestMethod.POST)
    public void getUserList(HttpServletRequest req,HttpServletResponse resp)
    {
		System.out.println("getUserList method ...");
		HttpSession session=req.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		int communityId=admin.getCommunityId();
		System.out.println("communityId:"+communityId);
        String name=req.getParameter("search_name");
        String telephoneNum=req.getParameter("search_phone");
        String buildingNo=req.getParameter("search_building");
        String roomNo=req.getParameter("search_room");
        String pageStr=req.getParameter("page");
        System.out.println("name:"+name+","+"telephoneNum:"+telephoneNum+",buildingNo:"+buildingNo+",roomNo:"+roomNo+",pageStr:"+pageStr);
        int pageIndex;
        if(pageStr==null)
        	pageIndex=1;
        else 
        	pageIndex=Integer.parseInt(req.getParameter("page"));
        UserService userSer=new UserService();
        List<User> userList=userSer.getUserList(name,telephoneNum,buildingNo,roomNo,pageIndex,communityId);
        int count=userSer.getUserCount(name, telephoneNum, buildingNo, roomNo,communityId);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("content",userList);
        jsonObject.put("count",count);
        System.out.println(jsonObject);
        PrintWriter out=null;
        try {
			out=resp.getWriter();
			out.print(jsonObject);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
        finally
        {
        	if(out!=null)
        	{
        		out.close();
        	}
        }
    }
	
	@RequestMapping(value="/pc/add_user", method = RequestMethod.POST)
    public void addUser(HttpServletRequest req,HttpServletResponse resp)
    {
		System.out.println("addUser method....");
		HttpSession session=req.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		int communityId=admin.getCommunityId();
		String name=req.getParameter("name");
		String telephoneNum=req.getParameter("telephoneNum");
		int buildingNo=Integer.parseInt(req.getParameter("building"));
		int roomNo=Integer.parseInt(req.getParameter("room"));
		int type=Integer.parseInt(req.getParameter("type"));
		System.out.println("name:"+name+",telephoneNum:"+telephoneNum+",buildingNo:"+buildingNo+",roomNo:"+roomNo+",type:"+type);
		UserService userSer=new UserService();
		boolean success=userSer.addUser(name, telephoneNum, type, buildingNo, roomNo,communityId);
        PrintWriter out=null;
        try {
			out=resp.getWriter();
			out.print(success);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
        finally
        {
        	if(out!=null) out.close();
        }
        
    }
	
	@RequestMapping(value="/pc/delete_user", method = RequestMethod.POST)
    public void deleteUser(HttpServletRequest req,HttpServletResponse resp)
    {
		System.out.println("deleteUser method....");
		int id=Integer.parseInt(req.getParameter("id"));
		UserService userSer=new UserService();
		boolean res=userSer.deleteUserById(id);
		if(res) System.out.println("删除成功");
		else System.out.println("删除失败");
		PrintWriter out=null;
		try {
			out=resp.getWriter();
			out.print(res);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			if(out!=null) out.close();
		}
    }
	
	@RequestMapping(value="/pc/update_user", method = RequestMethod.POST)
    public void updateUser(HttpServletRequest req,HttpServletResponse resp)
    {
		System.out.println("updateUser method ...");
		int id=Integer.parseInt(req.getParameter("id"));
        String name=req.getParameter("name");
        String telephoneNum=req.getParameter("telephoneNum");
        int buildingNo=Integer.parseInt(req.getParameter("building"));
        int roomNo=Integer.parseInt(req.getParameter("room"));
        int type=Integer.parseInt(req.getParameter("type"));
        System.out.println("id:"+id+",name:"+name+","+"telephoneNum:"+telephoneNum+",buildingNo:"+buildingNo+",roomNo:"+roomNo+",type:"+type);
        HouseService houseSer=new HouseService();
        UserService userSer=new UserService();
        boolean res=false;
        int houseId=houseSer.getHouseId(buildingNo,roomNo);
        if(houseId<0) 
        	System.out.println("房间号或者楼宇号有错误！！！");
        else
        {
        	res=userSer.updateUser(id, telephoneNum, name, type, houseId);
        }
        PrintWriter out=null;
        try {
			out=resp.getWriter();
			out.print(res);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
        finally
        {
        	if(out!=null) out.close();
        }
    }
	
	//获取用户的信息
	@RequestMapping(value="/wechat/get_user_info", method = RequestMethod.POST)
    public void getUserInfo(HttpServletRequest req,HttpServletResponse resp)
    {
		JSONObject jsonObject=new JSONObject();
		HttpSession session=req.getSession();
//		String nickname=session.getAttribute("nickname").toString();
//		String headimgurl=session.getAttribute("headimgurl").toString();
		String headimgurl="http://wx.qlogo.cn/mmopen/YIIf2XeguAljPUliaE9VJoKJNQWgGIJSvTniajpU7yCBPmp2kNvm8aVwAiakefEqCw4bxCACggNicXBROguEmh4h6UhlHzV8XRUN/0";
    	String nickName="乌云";
		jsonObject.put("nickname", nickName);
		jsonObject.put("headimgurl",headimgurl);
		PrintWriter out=null;
		try {
			out=resp.getWriter();
			out.print(jsonObject.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			if(out!=null) out.close();
		}
    }
}
